class Solution
{
public:
    int maximumSum(vector<int> &nums)
    {
        unordered_map<int, vector<int>> groups;
        for (int num : nums)
        {
            groups[digitSum(num)].push_back(num);
        }
        int answer = -1;
        for (auto &[_, group] : groups)
        {
            if (group.size() < 2)
            {
                continue;
            }
            sort(group.begin(), group.end());
            auto it = group.rbegin();
            auto it2 = it++;
            answer = max(answer, *it + *it2);
        }
        return answer;
    }

    int digitSum(int x)
    {
        int result = 0;
        while (x)
        {
            result += x % 10;
            x /= 10;
        }
        return result;
    }
};